package com.realtime.sys.service;

import com.alibaba.fastjson.JSONObject;
import com.realtime.sys.entity.UiBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class GetDataService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

//    public String getData() {
//        HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
//        // 获取 Redis 数据库中键为 bussiness::order::total 的数据
//        Map<String, String> testData = hashOps.entries("bussiness::order::total");
//        String[] produceId = new String[10];
//        String[] producetSumPrice = new String[10];
//        int i = 0;
//        // 封装数据
//        for (Map.Entry<String, String> entry : testData.entrySet()) {
//            produceId[i] = entry.getKey();
//            producetSumPrice[i] = entry.getValue();
//            i++;
//        }
//        UiBean ub = new UiBean();
//        ub.setProducetSumPrice(producetSumPrice);
//        ub.setProduceId(produceId);
//        // 将 ub 对象转换为 Json 格式的字符串
//        System.out.println(JSONObject.toJSONString(ub));
//        return JSONObject.toJSONString(ub);
//    }
public String getData() {
    HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
    Map<String, String> testData = hashOps.entries("bussiness::order::total");

    List<String> produceIdList = new ArrayList<>();
    List<String> producetSumPriceList = new ArrayList<>();
    for (Map.Entry<String, String> entry : testData.entrySet()) {
        produceIdList.add(entry.getKey());
        producetSumPriceList.add(entry.getValue());
    }

    UiBean ub = new UiBean();
    // 转换为数组（或直接设置 List，需 UiBean 支持）
    ub.setProduceId(produceIdList.toArray(new String[0]));
    ub.setProducetSumPrice(producetSumPriceList.toArray(new String[0]));

    return JSONObject.toJSONString(ub);
}
}
